/*
 * @Author: 莫莫
 * @Phone: 13450159681
 * @Description: 
 * @Date: 2024-08-08 15:03:49
 * @LastEditTime: 2024-12-25 22:38:16
 */
import {
	createStore
} from 'vuex';
// import { hexMD5 } from "../static/js/md5";

function token() {
	return wx.getStorageSync('token')
}
export default createStore({
	state: { //存放状态
		url: 'https://jiaju.etor.com.cn/index.php/addons/shopro/',
		storage: "https://jiaju.etor.com.cn/",
		api: {
			login: 'user/login', //登录接口,
			index: 'index/index', //首页接口,
			article: 'article/lists', //文章列表接口,
			articleDetail: 'article/details', //文章详情接口,
			category: {
				index:'category/index', //分类列表接口,
				banner:'category/banner', //分类广告接口,
			},
			filtrate: 'filtrate/index', //分类列表接口,
			goods: {
				index: 'goods.goods/index', //商品列表接口,
				detail: 'goods.goods/detail', //商品详情接口,
			},
			user: {
				goods_log: {
					favorite: 'user.goods_log/favorite', //商品收藏取消收藏接口,
					index: 'user.goods_log/index', //商品浏览,收藏记录接口，type=views为浏览记录，type=favorite为收藏记录,
					view_del: 'user.goods_log/viewDel', //删除浏览记录接口,goods_id为商品id,
				},
				user:{
					profile:'user.user/profile', //用户信息接口,
				}
			},
			third: {
				wechat: {
					getSessionId: 'third.wechat/getSessionId', //微信登录接口,
					login: 'third.wechat/login', //微信登录接口,
				},
			},
			appointment:'index/appointment', //预约接口,{name:姓名,phone:手机号,region:'广东省广州市天河区',address:详细地址,appointment_time:'2024-12-18'}}
		}
	},
	mutations: {
		tip(state, title, icon = 'none') {
			uni.showToast({
				title,
				icon,
				mask: true
			})
		},
		jump(state, url) {
			uni.navigateTo({
				url
			})
		}
	},
	actions: {
		request(content, data) {
			return new Promise((resolve) => {
				uni.request({
					url: content.state.url + data.url,
					method: data.method || "GET",
					header: {
						// "ucodelosan":token(),
						'Access-Control-Allow-Origin': '*',
						'Accept': 'application/json',
						'Content-Type': data.method == 'GET' ? 'application/json' : 'application/x-www-form-urlencoded',
						'token': token(),
					},
					data: data.data,
					success: (res) => {
						if (res.header.token) {
							uni.setStorageSync('token', res.header.token);
						  }
						if (res.data.code == 401) {
							uni.navigateTo({
								url: '/pages/login/login'
							})
							return
						}
						if (res.data.code == 1) {
							resolve(res.data)
						} else {
							this.commit('tip', res.data.msg)
						}
					}
				})
			})
		},
		upload(content, data) {
			console.log(data);
			return new Promise((resolve) => {
				uni.uploadFile({
					url: content.state.url + content.state.api.uploadImages,
					name: "image",
					filePath: data,
					formData: {
						uid: getId()
					},
					success(res) {
						let obj = JSON.parse(res.data)
						if (obj.code == 1) {
							resolve(obj.avatar)
						} else {
							content.commit('tip', '系统错误')
						}
					},
					fail(res) {
						console.log(res);
					}
				})
			})
		}
	}
})